<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 2018/12/28
 * Time: 20:13
 */

namespace app\index\model;


use think\Model;

class Sulintime extends Model
{

    /*
     * 获得对的胰岛素基础总量
     * @param int $uid
     * @param string $sdatetime  查询记录的日期
     * @return model|false $st
     * */
    public function getSulintime($uid,$sdatetime=''){
        
        if (empty($sdatetime)){
            
            $sdatetime = time();
        }
        $sdatetime = strtotime('+1day',$sdatetime);//改变当天也要起效
        $st = $this->where('uid',$uid)->where('update_time','<',$sdatetime)->order('update_time desc')->find();
        if (empty($st)){
            $st = $this->where('uid',$uid)->order('update_time asc')->find();
            return $st;
        }else{
            return $st;
        }
    }


    /*
     * 插入胰岛素基础总量的记录
     * @param int $uid
     * @param string $sulin0003
     * @param string $sulin0307
     * @param string $sulin0712
     * @param string $sulin1217
     * @param string $sulin1722
     * @param string $sulin2224
     * @param string $sdatetime  插入时间
     * */
    public function insertSulintime($uid,$sulin0003,$sulin0307,$sulin0712,$sulin1217,$sulin1722,$sulin2224,$update_time){
        $data  = array(
            'uid' => $uid,
            'sulin0003' => $sulin0003,
            'sulin0307' => $sulin0307,
            'sulin0712' => $sulin0712,
            'sulin1217' => $sulin1217,
            'sulin1722' => $sulin1722,
            'sulin2224' => $sulin2224,
            'update_time' => $update_time
        );
        $sdatetime = strtotime('+1day',time());//改变当天也要起效

        $st = $this->where('uid',$uid)->where('update_time','<',$sdatetime)->order('update_time desc')->find();
        
        if ($st['sulin0003'] == $sulin0003 && $st['sulin0307'] == $sulin0307 &&$st['sulin0712'] == $sulin0712 &&
            $st['sulin1217'] == $sulin1217 && $st['sulin1722'] == $sulin1722 &&$st['sulin2224'] == $sulin2224
        ){
            //没有改变的话，不需要修改
            return false;
        } 
        
        $r = $this->isUpdate(false)->save($data);
        if ($r!=false){
            return false;
        }

        return $r;



    }


}